﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SensorMonitor
{
    class Make
    {
        private static int SIZE_SEG = 64 * 2;
        private static int SIZE_TEXT_SEG = 64 * 2;
        private static int IdCAT = 0;       //IdCAT：第几个CAT通信。连续性，结合其他参数可用于判断通信质量


        public static String[] Msg(KHEAD head, KBIN bin, String txt){     //, String desFilename, String desId)
        {
            int width;
            width = 1 + bin.getLen() + txt.Length;

            byte[] b = new byte[width];

            b[0] = bin.getLen();
            byte offset = bin.copyTo(b, 1);
            txt.ToArray();
            
            bin.copy(txt, b, offset);


            //PP.bitWITH...
            if(txt.Equals("") == false){
                head.setBitPP(KConst.PP.bitWITH_BIN, true);
            }

            if(bin.getLen()>0){
                head.setBitPP(KConst.PP.bitWITH_STR, true);
            }


            //是否"文件传输"
            //bool withFILE_SYS = true;
            //if (desFilename == null) desFilename = "";
            //if (desFilename.Equals("")) withFILE_SYS = false;
            //m.setBitPP(KConst.PP.bitFILE_SYS, withFILE_SYS);


            //是否"级联"
            int size;
            if (txt == null) { size = 1; }
            else { size = ((txt.Length + bin.getLen()) / SIZE_SEG) + 1; }
            //
            if (size > 1){
                head.setBitPP(KConst.PP.bitCAT, true);
            }else{
                head.setBitPP(KConst.PP.bitCAT, false);
            }


            //============== Loop ===============
            String[] textSeg = new String[size];
            String[] seg = new String[size];
            KMsg8 m;

            for (int i = 0; i < size; i++){
                if (size == 1){
                    //txt = InsertBefore(desId, m, txt);
                    m.setBody(bin.getMsg(), txt);
                }
                else
                {   //seperator: "|"
                    if (i < (size - 1)){
                        textSeg[i] = txt.Substring(SIZE_TEXT_SEG * i, SIZE_TEXT_SEG);
                    }else{
                        textSeg[i] = txt.Substring(SIZE_TEXT_SEG * i);
                    }

                    //插入"dst="
                    textSeg[i] = InsertBefore(desId, m, textSeg[i]);
                    //
                    byte[] b = new byte[4];
                    b[0] = 0;
                    b[1] = (byte)IdCAT;
                    b[2] = (byte)i;
                    b[3] = (byte)size;

                    //String sIndex = String.valueOf(i);
                    //String sSize = String.valueOf(size);
                    //String sIdCAT = String.valueOf(IdCAT);      //本节点发出的，多包传输序号标识。暂为0
                    //“|”参数分隔符； “^”:区分隔符
                    //m.setBody(desFilename + "|" + sIdCAT + "|" + sIndex + "|" + sSize + "^" + textSeg[i], null, 0); 

                    m.setBitPP(KConst.PP.bitWITH_BIN, true);
                    //StrBytes.Hex2Bytes(sF);

                    if ((textSeg[i] == "") || (textSeg[i] == null))
                    {
                        m.setBitPP(KConst.PP.bitWITH_STR, false);
                    }
                    m.setBody(b, textSeg[i]);
                    //
                    //System.out.println(textSeg[i]);
                    //System.out.println(String.valueOf(i) + "/" + String.valueOf(size) + ":" + textSeg[i]);
                }

                m.make();

                int len = m.getMsg().Length;
                //String nodeIndex = String.valueOf(Index);       //
                seg[i] = StrBytes.Bytes2Hex(m.getMsg(), len);    //本地不添加attach      // + "<" + knode.getId() + "_" + nodeIndex;       //....<1038_3748
            }
            return seg;
        }            





        public static String[] MsgText(KMsg8 m, String sInst, String desFilename, String desId){
            //
            m.setVer(KMsg8.VER_MARK);
            m.setLen(0);
            m.setId("0");
            //m.setOp((byte)0);
            //m.setIndex((byte)0);
        
            //PP.bitWITH...
            m.setBitPP(KConst.PP.bitWITH_STR, true);
            m.setBitPP(KConst.PP.bitWITH_BIN, true);
        
            //是否"文件传输"
            bool withFILE_SYS = true;
            if(desFilename == null)desFilename = "";
            if(desFilename.Equals(""))withFILE_SYS = false;
            m.setBitPP(KConst.PP.bitFILE_SYS, withFILE_SYS);
        
            //是否"级联"
            int size;
            if(sInst == null){size = 1;}
            else{size = (sInst.Length/SIZE_TEXT_SEG) + 1;}    
            //
            if(size > 1){
                m.setBitPP(KConst.PP.bitCAT, true); }
            else{
                m.setBitPP(KConst.PP.bitCAT, false); }

            //============== Loop ===============
            String[] textSeg = new String[size];
            String[] seg = new String[size];
   
            for(int i = 0; i < size; i++){
                if(size == 1){
                    sInst = InsertBefore(desId, m, sInst);
                    //
                    m.setBitPP(KConst.PP.bitWITH_BIN, false);
                    if((sInst == "")||(sInst == null)){
                        m.setBitPP(KConst.PP.bitWITH_STR, false);}
                    m.setBody(null, sInst); }       
                else{   //seperator: "|"
                    if(i == (size -1)){ 
                        textSeg[i] = sInst.Substring(SIZE_TEXT_SEG * i); }
                    else{ 
                        textSeg[i] = sInst.Substring(SIZE_TEXT_SEG * i, SIZE_TEXT_SEG); }       //sInst.Substring(SIZE_TEXT_SEG * i, SIZE_TEXT_SEG * (i+1)); }
                
                    //插入"dst="
                    textSeg[i] = InsertBefore(desId, m, textSeg[i]);
                    //
                    byte[] b = new byte[4];
                    b[0] = 0;
                    b[1] = (byte)IdCAT;
                    b[2] = (byte)i;
                    b[3] = (byte)size;
                
                    //String sIndex = String.valueOf(i);
                    //String sSize = String.valueOf(size);
                    //String sIdCAT = String.valueOf(IdCAT);      //本节点发出的，多包传输序号标识。暂为0
                    //“|”参数分隔符； “^”:区分隔符
                    //m.setBody(desFilename + "|" + sIdCAT + "|" + sIndex + "|" + sSize + "^" + textSeg[i], null, 0); 
                
                    m.setBitPP(KConst.PP.bitWITH_BIN, true);
                    //StrBytes.Hex2Bytes(sF);

                    if((textSeg[i] == "")||(textSeg[i] == null)){
                        m.setBitPP(KConst.PP.bitWITH_STR, false);}
                    m.setBody(b, textSeg[i]); 
                    //
                    //System.out.println(textSeg[i]);
                    //System.out.println(String.valueOf(i) + "/" + String.valueOf(size) + ":" + textSeg[i]);
                }  
            
                m.make(); 

                int len = m.getMsg().Length;
                //String nodeIndex = String.valueOf(Index);       //
                seg[i] = StrBytes.Bytes2Hex(m.getMsg(), len);    //本地不添加attach      // + "<" + knode.getId() + "_" + nodeIndex;       //....<1038_3748
            }
            return seg;
        }            

        
        //desFilename == null, desId == null
        public static String[] MsgText(KMsg8 m, String sInst){
            return MsgText(m, sInst, null, null);
        }
    
        public static String[] MsgArray(KMsg8 m, byte[] array){
            m.setBitPP(KConst.PP.bitWITH_STR, false);
            m.setBitPP(KConst.PP.bitWITH_BIN, true);
        
            m.setBody(array, null);
            m.make(); 

            int len = m.getMsg().Length;
            String[] s = new String[1];
            s[0] = StrBytes.Bytes2Hex(m.getMsg(), len);    //本地不添加attach      // + "<" + knode.getId() + "_" + nodeIndex;       //....<1038_3748
            return s;    
        }
    
        
        //在sText前insert "dst="
        private static String InsertBefore(String desId, KMsg8 m, String sText){
            //"dst=1029;(分号)"
            if(desId == null){
                m.setBitPP(KConst.PP.bitWITH_DEST, false);
            }
            else{
                m.setBitPP(KConst.PP.bitWITH_DEST, true);
                sText = "dst=" + desId + "^" + sText;
            }
            return sText;
        }
    
    
        private static String getDestStr(String desId, KMsg8 m){
            //"dst=1029;(分号)"
            String sDest;
            if(desId == null){
                m.setBitPP(KConst.PP.bitWITH_DEST, false);
                sDest = "";
            }
            else{
                m.setBitPP(KConst.PP.bitWITH_DEST, true);
                sDest = "dst=" + desId;
            }
            return sDest;
        }
    

        //保留：原op\index；
        //设置：bitINST_DIR = 1
        public static String MsgAck(KMsg8 m, String new_Id, String new_sBody, byte[] new_bBody, int new_bBody_len){
            //m.setLen(KConst.LEN_HEAD32);
            //m.setOpType(op);          //原op
            m.setId(new_Id);            //new id
            //m.setIndex(index);        //原顺序号
        
            //Ack
            bool fDirAck = true;
            m.setBitPP(KConst.PP.bitINST_DIR, fDirAck);     //bit = 1;
            m.setBitPP(KConst.PP.bitRESULT, false);
        
            m.setBody(new_bBody, new_sBody);    
            m.make();

            int len = m.getMsgLen();
            String s = StrBytes.Bytes2Hex(m.getMsg(), len);
            //String s = StrBytes.Bytes2Hex(m.getMsg(), KConst.LEN_HEAD32);
            return s;
        }
    
        //保留：原op\index；
        //设置：bitRESULT = 1
        public static String MsgResult(KMsg8 m, String new_Id, String new_sBody, byte[] new_bBody, int new_bBody_len){
            //m.setLen(KConst.LEN_HEAD32);
            //m.setOpType(op);          //原op
            m.setId(new_Id);       //new id
            //m.setIndex(index);        //原顺序号
        
            //Back
            m.setBitPP(KConst.PP.bitRESULT, true);     //bit = 1;
        
        
            m.setBody(new_bBody, new_sBody);
            m.make();

            int len = m.getMsgLen();
            String s = StrBytes.Bytes2Hex(m.getMsg(), len);
            //String s = StrBytes.Bytes2Hex(m.getMsg(), KConst.LEN_HEAD32);
            return s;
        }
    
        public static String MsgEcho(){
            return "I am the echo !";
        }

















    }
}
